define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'replace/clubs/index' + location.search,
                    add_url: 'replace/clubs/add',
                    to_examine_url: 'replace/clubs/to_examine',
                    del_url: 'replace/clubs/del',
                    multi_url: 'replace/clubs/multi',
                    import_url: 'replace/clubs/import',
                    table: 'd_class_hour',
                }
            });

            var table = $("#table");
            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text(" 导出");
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                escape:false,
                exportTypes: [ 'csv', 'excel'],//设置导出格式
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),  //导出表格名称
                    ignoreColumn: [0, 'operate','first_trial_admin_signature','end_trial_admin_signature'], //不导出字段
                    ignoreRow: [],
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'times', title: __('Select_time'), operate:'LIKE'},
                        {field: 'num', title: __('Num'), operate:false},
                        // {field: 'course.name', title: __('Course_type_id'), operate:false},
                        {field: 'admin.nickname', title: '提交人', operate:'LIKE'},
                        {field: 'clubs.class_name', title: '社团', operate:'LIKE'},
                        {field: 'identity', title: '身份', operate:'LIKE'},
                        {field: 'teacher.nickname', title: '被代课人', operate:false},
                        {field: 'public_leave', title: __('公假代'),searchList: {0: '否', 1: '是'},formatter: Table.api.formatter.status},
                        {field: 'status', title: __('状态'),searchList: {"0": '待提交',"1": '等待被代人提交', "2": '已提交','3':'初审通过/终审待审核','4':'初审已驳回','5':'终审已通过','6':'终审已驳回'},formatter: Table.api.formatter.status,custom:{ 1:'danger',2:'warning',3:'warning',4:'danger',5:'success',6:'danger'}},
                        {field: 'remarks', title: __('Remarks'), operate:false},
                        {field: 'first_trial_admin_name', title: '初审人员', operate:false},
                        {field: 'first_trial_admin_signature', title: '初审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'first_trial_time', title: '初审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'first_trial_reject_reason', title: '初审驳回原因', operate:false},
                        {field: 'end_trial_admin_name', title: '终审人员', operate:false},
                        {field: 'end_trial_admin_signature', title: '终审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'end_trial_time', title: '终审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_trial_reject_reason', title: '终审驳回原因', operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                // {
                                //     name: 'to_examine',
                                //     text: '提交审核',
                                //     confirm: '确定操作吗？',
                                //     icon: '',
                                //     classname: 'btn btn-xs btn-primary btn-ajax',
                                //     url: 'replace/clubs/to_examine',
                                //     success: function (data, ret) {
                                //         $(".btn-refresh").trigger("click");
                                //         //如果需要阻止成功提示，则必须使用return false;
                                //         //return false;
                                //     },
                                // },

                                {
                                    name: 'to_examine',
                                    text: '提交审核',
                                    hidden:function(row){
                                        if(row.status == 0 && row.current_admin_id == row.admin.id){
                                            return false; //或者return false
                                        }else{
                                            return true;
                                        }
                                    },
                                    confirm: '确定操作吗？',
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/to_examine?type=1',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                                {
                                    name: 'del',
                                    text: '删除',
                                    hidden:function(row){
                                        if(row.status != 1 && row.status != 4 && row.status != 6){
                                            return true; //或者return false
                                        }
                                    },
                                    confirm: '确定操作吗？',
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/del',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },{
                                    name: 'to_examine',
                                    text: '取消提交',
                                    hidden:function(row){
                                        if(row.status != 0 && row.status != 5 && row.status != 4 && row.status != 6 && row.current_admin_id == row.admin.id){
                                            return false; //或者return false
                                        }else{
                                            return true;
                                        }
                                    },
                                    confirm: '确定操作吗？',
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/to_examine?type=2',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                            ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            formatter: function (value, row, index) { //隐藏自定义的视频按钮
                                var admin_id = Config.admin;
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                //权限判断
                                if(row.status == 2 || row.status == 3 || row.status == 5){ //通过Config.chapter 获取后台存的chapter
                                    // $(table).data("operate-del", null);
                                }
                                if(row.status != 1){ //通过Config.chapter 获取后台存的chapter
                                    // $(table).data("operate-to_examine", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        cover_index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'replace/clubs/cover_index' + location.search,
                    add_url: 'replace/clubs/add',
                    to_examine_url: 'replace/clubs/to_examine',
                    del_url: 'replace/clubs/del',
                    multi_url: 'replace/clubs/multi',
                    import_url: 'replace/clubs/import',
                    table: 'd_class_hour',
                }
            });

            var table = $("#table");
            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text(" 导出");
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                escape:false,
                exportTypes: [ 'csv', 'excel'],//设置导出格式
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),  //导出表格名称
                    ignoreColumn: [0, 'operate','first_trial_admin_signature','end_trial_admin_signature'], //不导出字段
                    ignoreRow: [],
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'times', title: __('Select_time'), operate:'LIKE'},
                        {field: 'num', title: __('Num'), operate:false},
                        // {field: 'course.name', title: __('Course_type_id'), operate:false},
                        {field: 'admin.nickname', title: '提交人', operate:'LIKE'},
                        {field: 'clubs.class_name', title: '社团', operate:'LIKE'},
                        {field: 'identity', title: '身份', operate:'LIKE'},
                        {field: 'teacher.nickname', title: '被代课人', operate:false},
                        {field: 'public_leave', title: __('公假代'),searchList: {0: '否', 1: '是'},formatter: Table.api.formatter.status},
                        {field: 'status', title: __('状态'),searchList: {"1": '待提交', "2": '已提交','3':'初审通过/终审待审核','4':'初审已驳回','5':'终审已通过','6':'终审已驳回'},formatter: Table.api.formatter.status,custom:{ 1:'danger',2:'warning',3:'warning',4:'danger',5:'success',6:'danger'}},
                        {field: 'remarks', title: __('Remarks'), operate:false},
                        {field: 'first_trial_admin_name', title: '初审人员', operate:false},
                        {field: 'first_trial_admin_signature', title: '初审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'first_trial_time', title: '初审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'first_trial_reject_reason', title: '初审驳回原因', operate:false},
                        {field: 'end_trial_admin_name', title: '终审人员', operate:false},
                        {field: 'end_trial_admin_signature', title: '终审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'end_trial_time', title: '终审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_trial_reject_reason', title: '终审驳回原因', operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                {
                                    name: 'to_examine',
                                    text: '确认提交审核',
                                    hidden:function(row){
                                        if(row.status != 1 || row.current_admin_id != row.teacher.id){
                                            return true; //或者return false
                                        }
                                    },
                                    confirm: '确定操作吗？',
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/to_examine',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                                {
                                    name: 'del',
                                    text: '删除',
                                    hidden:function(row){
                                        if(row.status != 1 && row.status != 4 && row.status != 6){
                                            return true; //或者return false
                                        }
                                    },
                                    confirm: '确定操作吗？',
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/del',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                            ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        auditing_index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'replace/clubs/auditing_index' + location.search,
                    add_url: 'replace/clubs/add',
                    to_examine_url: 'replace/clubs/to_examine',
                    multi_url: 'replace/clubs/multi',
                    import_url: 'replace/clubs/import',
                    table: 'd_class_hour',
                }
            });

            var table = $("#table");
            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text(" 导出");
                }
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                escape:false,
                exportTypes: [ 'csv', 'excel'],//设置导出格式

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'times', title: __('Select_time'), operate:'LIKE'},
                        {field: 'num', title: __('Num'), operate:false},
                        // {field: 'course.name', title: __('Course_type_id'), operate:false},
                        {field: 'admin.nickname', title: '提交人', operate:'LIKE'},
                        {field: 'clubs.class_name', title: '社团', operate:'LIKE'},
                        {field: 'identity', title: '身份', operate:'LIKE'},
                        {field: 'teacher.nickname', title: '被代课人', operate:false},
                        {field: 'public_leave', title: __('公假代'),searchList: {0: '否', 1: '是'},formatter: Table.api.formatter.status},
                        {field: 'status', title: __('状态'),searchList: {"1": '待提交', "2": '已提交','3':'初审通过/终审待审核','4':'初审已驳回','5':'终审已通过','6':'终审已驳回'},formatter: Table.api.formatter.status,custom:{ 1:'danger',2:'warning',3:'warning',4:'danger',5:'success',6:'danger'}},
                        {field: 'remarks', title: __('Remarks'), operate:false},
                        {field: 'first_trial_admin_name', title: '初审人员', operate:false},
                        {field: 'first_trial_admin_signature', title: '初审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'first_trial_time', title: '初审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'first_trial_reject_reason', title: '初审驳回原因', operate:false},
                        {field: 'end_trial_admin_name', title: '终审人员', operate:false},
                        {field: 'end_trial_admin_signature', title: '终审电子签章', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'end_trial_time', title: '终审时间',datetimeFormat:'YYYY-MM-DD', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_trial_reject_reason', title: '终审驳回原因', operate:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                {
                                    name: 'pass_through',
                                    text: '初审通过',
                                    confirm: '确定操作吗？',
                                    hidden:function(row){
                                        if(row.check_auth == 1 && row.status == 2){
                                            return false; //或者return false
                                        }else{
                                            return true;
                                        }
                                    },
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/pass_through',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                                {
                                    name: 'pass_through',
                                    text: '终审通过',
                                    confirm: '确定操作吗？',
                                    hidden:function(row){
                                        if(row.check_auth == 2 && row.status == 3){
                                            return false; //或者return false
                                        }else{
                                            return true;
                                        }
                                    },
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'replace/clubs/pass_through',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                },
                                {
                                    name: 'reject',
                                    text: '驳回',
                                    icon: '',
                                    hidden:function(row){
                                        if(row.check_auth == 1 && row.status == 2){
                                            return false; //或者return false
                                        }else if(row.check_auth == 2 && (row.status == 3 || row.status == 5)){
                                            return false; //或者return false
                                        }else{
                                            return true;
                                        }
                                    },
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'replace/clubs/reject',
                                },
                            ],
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        }
                    ]
                ]
            });


            $('.batch-examine').on('click', function() {
                var ids = Table.api.selectedids(table);
                var type = $(this).data('type');
                $.ajax({
                    url: 'replace/clubs/batch_examine',
                    type: 'POST',
                    data: {
                        ids: JSON.stringify(ids),
                        type:type
                    },
                    success: function(data) {
                        if(data.code === 1){
                            Toastr.success(data.msg);
                            setTimeout(function() {
                                $(".btn-refresh").trigger("click");
                            }, 300);
                        }else{
                            Toastr.error(data.msg);
                        }
                    },
                    error: function(xhr, status, error) {
                        Toastr.error('请求失败'); return false;
                    }
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        reject: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        to_examine: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $("#c_id").change(function () {
                    var name = $('#c_id').val();
                    $('#nickname').val('');
                    $('#clubs_id').val(0);
                    if(name){
                        $('#select_teacher').show();
                        Fast.api.teacher_Select('.select', {
                            multiple: false,
                            clubs_name:name,
                            callback: function (data) {
                                setTeacher(data);
                            }
                        });
                    }else{
                        $('#select_teacher').hide();
                    }
                });




                var setTeacher = function (data) {
                    if(data && data[0]){
                        $('#nickname').val(data[0].nickname);
                        $('#clubs_id').val(data[0].clubs_id);
                    }
                };
            }
        }
    };
    return Controller;
});
